package com.vains.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vains.dto.request.RolePermissionsReq;
import com.vains.entity.OauthRole;
import com.vains.entity.OauthRolePermission;
import com.vains.mapper.OauthRoleMapper;
import com.vains.service.IOauthRolePermissionService;
import com.vains.service.IOauthRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author vains
 * @since 2020-06-08
 */
@Slf4j
@Service
public class OauthRoleServiceImpl extends ServiceImpl<OauthRoleMapper, OauthRole> implements IOauthRoleService {

    private final IOauthRolePermissionService oauthRolePermissionService;

    public OauthRoleServiceImpl(IOauthRolePermissionService oauthRolePermissionService) {
        this.oauthRolePermissionService = oauthRolePermissionService;
    }

    @Override
    public List<OauthRole> getAllRoles(Page<OauthRole> page) {
        return baseMapper.selectPage(page, null).getRecords();
    }

    @Override
    @Transactional
    public void resetRolePermissions(RolePermissionsReq rolePermissionsReq) {
        Assert.notNull(rolePermissionsReq, "入参不能为空!");
        // 先删除角色和权限对应的关系
        Integer roleId = rolePermissionsReq.getRoleId();
        Assert.notNull(roleId, "角色ID不能为空!");
        // 判断数据库中是否存在
        QueryWrapper<OauthRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        log.info("查询角色 {} 对应的权限ID...", roleId);
        List<OauthRolePermission> rolePermissions = oauthRolePermissionService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(rolePermissions)) {
            log.info("角色之前已有权限, 删除原有记录...");
            // 如果数据库中存在就删除记录
            UpdateWrapper<OauthRolePermission> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("role_id", roleId);
            oauthRolePermissionService.remove(updateWrapper);
        }
        // 不存在就直接添加
        List<Integer> rolePermissionsId = rolePermissionsReq.getRolePermissions();
        if (CollectionUtils.isEmpty(rolePermissionsId)) {
            log.info("角色 {} 修改后的权限是空的,跳过添加过程.", roleId);
            // 如果角色ID是空的,则跳过后边的添加过程
            return;
        }
        // 待添加集合
        List<OauthRolePermission> userRoles = new ArrayList<>();
        // 如果角色ID不是空的,则添加记录
        for (Integer permissionId : rolePermissionsId) {
            if (permissionId == null) {
                // 如果角色ID是空的,则跳过
                continue;
            }
            OauthRolePermission rolePermission = new OauthRolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            userRoles.add(rolePermission);
        }
        if (!CollectionUtils.isEmpty(userRoles)) {
            log.info("开始赋予角色 {} 权限...", roleId);
            // 批量添加
            oauthRolePermissionService.saveBatch(userRoles);
        }
    }

    @Override
    public List<Integer> getPermissionsByRoleId(Integer roleId) {
        return baseMapper.getPermissionsByRoleId(roleId);
    }
}
